package practice.library.webmagic.processor.qyer

import us.codecraft.webmagic.{Page, Site}
import us.codecraft.webmagic.processor.PageProcessor
import utility.CustomLogger

/**
  * Created by lovepocky(lovepocky@foxmail.com) on 2016/6/17.
  */
class SubPlaceOfPlaceProcessor(val city: String) extends PageProcessor with CustomLogger {
	val site = Site.me().setRetryTimes(5).setSleepTime(1000)
	
	override def getSite: Site = site
	
	override def process(page: Page): Unit = {
		//Handle Current Page
		loggers.console.info(s"start to handle page: ${page.getUrl.toString }")
		
		import QyerCommon._
		if (page.getUrl.toString.matches(QyerCommon.api.city("\\w+"))) {
			loggers.console.info("handle in city")
			//page.putField("type", page.getHtml.css("head > meta:nth-child(10)"))
			page.putField("keywords", page.getHtml.css("meta[name=\"keywords\"]", "content"))
			page.putField("image_url", page.getHtml.css("meta[property=\"og:image\"]", "content"))
			page.putField("name_chs", page.getHtml.css("body > div.plcTopBar.clearfix > div.plcTopBarL > div > p > a", "text").replace(" ", ""))
			page.putField("name_en", page.getHtml.css("body > div.plcTopBar.clearfix > div.plcTopBarL > p > a", "text").replace(" ", ""))
			
			//add all travel page link 
			transform.cityUrl2City(page.getUrl.toString).foreach(it => page.addTargetRequest(api.allSubPlace(it)))
		} else if (page.getUrl.toString.matches(QyerCommon.api.allSubPlace("\\w+"))) {
			loggers.console.info("handle in all sub place")
			//page.getHtml.links().all().toArray(Array[String]()).toList.foreach(println)
			val maxPage = transform.findMaxPage(page.getHtml.links().regex(regexPattern.allSubPlacePages(city)).all().toArray(Array[String]()))
			//add all travel pages 
			import collection.JavaConversions._
			page.addTargetRequests(Range(1, 1 + 1).map(it => api.allSubPlacePages(city, it)))
		} else {
			loggers.console.info(page.getHtml.getDocument.body().toString)
		}
		loggers.console.info(page.getResultItems.toString)
	}
}

object SubPlaceOfPlaceProcessor {
	def apply(city: String): SubPlaceOfPlaceProcessor = new SubPlaceOfPlaceProcessor(city)
}